package com.hsmpay.common.util.bank;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取卡类型
 *
 * @author 颜铃璋
 * @version v1.0
 * @created 2013-10-23
 */
public class BankCardTypeUtil {
    static Logger log = Logger.getLogger(BankCardTypeUtil.class);
    /**
     * 卡bin Map  注意取的是 六位卡bean
     */
    public static Map<String, Object> cardBinMap = new HashMap<String, Object>();

    /**
     * 初始化 卡bin
     */
    public static void initCardBin() {
        try {
        	
            String filePath = BankCardTypeUtil.class.getResource("").getPath();
            String basePath = filePath.substring(0, filePath.indexOf("WEB-INF")) +"WEB-INF/classes/bankCardType.xls" ;
            //String basePath = BankCardTypeUtil.class.getClass().getResource("/").getPath().toString()+"bankCardType.xls";
            //basePath = basePath.replace("test-classes", "classes");
            
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(basePath));
            HSSFSheet sheet = workbook.getSheetAt(0);// 获得第一个sheet
            int lastRowNum = sheet.getLastRowNum();
            HSSFRow row = null;
            for (int rowNumOfSheet = 3; rowNumOfSheet <= lastRowNum; rowNumOfSheet++) {
                row = sheet.getRow(rowNumOfSheet);
                //log.debug("row："+row.getCell(1));
                if (null == row.getCell(0)) break;//####如果有第一列为空 那么后面的行就是没有数据的
                String bankName = row.getCell(1).getStringCellValue();
                String bankName2 = row.getCell(2).getStringCellValue();
                String cardBin = ((int) row.getCell(4).getNumericCellValue()) + "";
                String bankType = row.getCell(3).toString();//"借记卡";
			  
                if (bankType.indexOf("借记卡") > -1) {
                    continue;
                }
              
                if (!bankName2.contains("信用")  &&  !bankName2.contains("贷记") ) {
                    continue;
                }
                if (cardBin.length() < 6) {
                    cardBin = cardBin.substring(0, cardBin.length());
                } else {
                    cardBin = cardBin.substring(0, 6);
                }
                cardBinMap.put(cardBin, bankName);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 初始化 卡bin
     */
    static {
        initCardBin();
    }

    /**
     * 判断卡号  是不是信用卡
     *
     * @param bankCard
     * @return
     */
    public static boolean isCreditCard(String bankCard) {
        if (StringUtils.isEmpty(bankCard)) return false;
        int len = bankCard.length();
        String cardBin = bankCard.substring(0, 6);
        String bankName = null;
        switch (len) {
            case 15:
                return true;//十五位卡号  全部是信用卡
            case 16:
                if (bankCard.indexOf("4") == 0 || bankCard.indexOf("5") == 0) {//十六位 4、5开头的卡是信用卡
                    return true;
                } else if (bankCard.indexOf("6") == 0) {
                    //检索类库
                    bankName = (String) cardBinMap.get(cardBin);
                    if (null == bankName) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            default:
                return false;
        }
    }

//    public static void main(String args[]) {
//        /*String bankCard = "4567890123456";
//		log.debug(bankCard.indexOf("4"));
//		log.debug(BankCardTypeUtil.class.getResource("").getPath()+"bankCardType.xlsx");
//		initCardBin();*/
//        log.debug(isCreditCard("6255060018160376"));
//        log.debug(isCreditCard("6226550000757590"));
//        log.debug(isCreditCard("6225683721003102938"));
//        log.debug(isCreditCard("6225210606719806"));
//
//    }
}

